package me.goldze.mvvmhabit.base;

import android.app.Activity;
import android.app.ActivityManager;
import android.content.Context;
import android.util.Log;

import java.util.Stack;

/**
 * Created by Administrator on 2019/4/26.
 * 管理activity的任务栈
 */

public class AppManager {
    private static Stack<Activity> activityStack;
    private static AppManager instance;

    private AppManager() {
    }

    /**
     * 单一实例
     */
    public static AppManager getAppManager() {
        if (instance == null) {
            instance = new AppManager();
        }
        return instance;
    }
    /**
     * 移除指定的Activity
     */
    public void removeActivity(Activity activity) {
        if (activity != null) {
            activityStack.remove(activity);
        }
    }
    /**
     * 添加Activity到堆栈
     */
    public void addActivity(Activity activity) {
        Log.i("AppManager", "添加了activity" + activity.getClass().getCanonicalName());
        if (activityStack == null) {
            activityStack = new Stack<Activity>();
        }
        activityStack.add(activity);
    }

    /**
     * 获取当前Activity（堆栈中最后一个压入的）
     */
    public Activity currentActivity() {
        Activity activity = activityStack.lastElement();
        return activity;
    }

    public int getSize() {
        try {
            return activityStack.size();
        } catch (Exception e) {
            e.printStackTrace();
            return 0;
        }

    }

    /**
     * 结束当前Activity（堆栈中最后一个压入的）
     */
    public void finishActivity() {
        Activity activity = activityStack.lastElement();
        finishActivity(activity);
    }

    /**
     * 结束指定的Activity
     */
    public void finishActivity(Activity activity) {
        if (activity != null) {
            activityStack.remove(activity);
            activity.finish();
            activity = null;
        }
    }

    /**
     * 结束指定类名的Activity
     */
    public void finishActivity(Class<?> cls) {
        Stack<Activity> tempActivityStack=new Stack<>();
        for (Activity activity : activityStack) {
            if (activity.getClass().equals(cls)) {
//                finishActivity(activity);
                tempActivityStack.add(activity);
            }
        }
        activityStack.removeAll(tempActivityStack);
    }

    /**
     * 结束所有Activity
     */
    public void finishAllActivity() {
        for (int i = 0, size = activityStack.size(); i < size; i++) {
            if (null != activityStack.get(i)) {
                activityStack.get(i).finish();
            }
        }
        activityStack.clear();
    }

    /**
     * 退出应用程序
     */
    public void AppExit(Context context) {
        try {
            finishAllActivity();
            ActivityManager activityMgr = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
            activityMgr.restartPackage(context.getPackageName());
            System.exit(0);
        } catch (Exception e) {
        }
    }

    /**
     * 关闭除当前activity之外的所有Activity
     */
    public static void closeAllActivityExceptFoCurrentActivity() {
        // Log.i("aaa", "运行到这142" + getActivities().size());
        try {
            Activity[] act = new Activity[activityStack.size() - 1];//
            Log.i("aaa", "" + (activityStack.size()));
            for (int i = 0; i < activityStack.size() - 1; i++) {
                act[i] = activityStack.get(i);
                Log.i("aaa", "i为" + i);
            }
            for (int i = 0; i < act.length; i++) {
                if (act[i] != null) {
                    act[i].finish();
                }
            }
        } catch (Exception e) {
            // TODO: handle exception
        }

    }

    /**
     * 从第二个activity开始关闭，直到关闭倒数第二个activity，已验证 通常用于回到首页（调用这个方法之后再调用finish就能回到首页）
     *
     * @param
     */
    public void close_0_sizejian2() {
        try {

            Log.i("aaa", "栈的大小===》" + activityStack.size());
            // 如果activity的大小小于等于二
            if (activityStack.size() <= 2) {
                Log.i("aaa", "栈的大小116===》" + activityStack.size());
                return;
            }
        } catch (Exception e) {
            // TODO: handle exception
            Log.i("aaa", "栈的大小121===》" + activityStack.size());
        }

        try {
            // 如果是5个activity 要去掉3个activity
            Activity[] act = new Activity[activityStack.size() - 2];
            Log.i("aaa", "栈的大小127===》" + activityStack.size());
            for (int i = 1; i < activityStack.size() - 1; i++) {
                act[i - 1] = activityStack.get(i);
            }
            for (int i = 0; i < act.length; i++) {
                if (act[i] != null) {
                    act[i].finish();
                }
            }
            Log.i("aaa", "栈的大小136===》" + activityStack.size());
        } catch (Exception e) {
            // TODO: handle exception
            Log.i("aaa", "栈的大小139===》" + activityStack.size());
        }
    }
}
